﻿using System;
using System.IO;
using System.Text;

namespace Ehai.Web.Framework.Rules
{
    class HtmlRuleHandle : IMsgRuleHandle
    {
        #region IMsgRuleHandle 成员

        public const string Name = "html";

        private int _execStatus = 0;
        public int ExecStatus
        {
            get { return _execStatus; }
        }

        /// <summary>
        /// 输入文件路径及文件名,返回html内容
        /// </summary>
        /// <param name="p">
        /// 传入数组<br /> 
        /// 0:(必选)处理类型,html<br /> 
        /// 1:(必选)要返回的Html文件名<br /> 
        /// 2:(可选)要返回的Html文件路径(不包含文件名)<br /> 
        /// </param>
        /// <param name="ci">环境变量</param>
        /// <example>
        /// string[] p = {"html","try2.htm","D:\\ehai2009实践\\7.28\\"};<br />
        /// </example>
        /// <example>
        /// string[] p = {"html","try2.htm",""};<br />
        /// </example>
        /// <example>
        /// string[] p = {"html","try1.htm",""};<br />
        /// </example>
        /// <exception cref="Ehai.Web.Framework.WebException">
        /// Ehai.Web.Framework.WebException:文件读取错误！
        /// </exception>
        /// <returns>返回指定的html内容</returns>
        public string[][] Execute(string[] p, ContextInfo ci)
        {

            if (p.Length < 2 || p[1].Trim() == String.Empty)
            {
#if DEBUG
                // 操作失败,状态值为1
                _execStatus = 1;
#endif
                return null;
            }

            string path = p.Length < 3 ? ci.ProjectPath : p[2].Trim();
            string file = Path.Combine(path, p[1].Trim());
            string re = String.Empty;

            try
            {
                if (!File.Exists(file))
                {
#if DEBUG
                // 操作失败并捕获异常,状态值为-1
                    _execStatus = -1;
#endif
                    throw new Exception("请输入正确的文件路径！！");
                }
                re = Ehai.Web.Framework.FileHandle.ReadFile(file, Encoding.Default);
#if DEBUG
                // 操作成功,状态值为0
                _execStatus = 0;
#endif               
                return new string[1][] { new string[] { re } };

            }
            catch (Exception ex)
            {
#if DEBUG
                // 操作失败并捕获异常,状态值为-1
                _execStatus = -1;
#endif
                throw new Ehai.Web.Framework.WebException("Ehai.WebFramework.HtmlRule", "文件读取错误！", ex);
            }
        }

        public string GetHelpInfo()
        {
            return  "html内容";
        }

        public string GetHelpDetail()
        {
            return  "输入文件路径及文件名,返回html内容:<br />" +
                    "例1: { html, try2.htm, D:\\ehai2009实践\\7.28 }; //输入绝对路径,返回正确结果<br /> " +
                    "例2: { html, try2.htm }; //输入相对路径,返回正确结果<br /> " +
                    "例3: { html, try1.htm }; //文件不存在,抛出异常<br /> " +
                    "返回:指定的html内容";
        }

        public string[] GetParametersInfo()
        {
            string[] t = new string[3];
            t[0] = "(必选)处理类型,即\"html\"";
            t[1] = "(必选)要返回的Html文件名";
            t[2] = "(可选)要返回的Html文件路径(不包含文件名)";
            return t;
        }

        #endregion
    }
}
